package com.yugao.fintech.antelope.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yugao.fintech.antelope.shop.assembly.SkuStockInOutDetailAssembly;
import com.yugao.fintech.antelope.shop.manager.SkuStockInOutDetailManager;
import com.yugao.fintech.antelope.shop.mapper.SkuStockInOutDetailMapper;
import com.yugao.fintech.antelope.shop.model.entity.SkuStockInOutDetail;
import com.yugao.fintech.antelope.shop.model.rqrs.sku.SkuStockInOutDetailPageReq;
import com.yugao.fintech.antelope.shop.model.rqrs.sku.SkuStockInOutDetailResp;
import com.yugao.fintech.antelope.shop.model.rqrs.sku.SkuStockInOutDetailSaveReq;
import com.yugao.fintech.antelope.shop.model.rqrs.sku.SkuStockInOutDetailUpdateReq;
import com.yugao.fintech.antelope.shop.service.SkuStockInOutDetailService;
import com.yugao.fintech.framework.mybatisplus.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * 出入库明细
 *

 */
@Slf4j
@Service
public class SkuStockInOutDetailServiceImpl extends ServiceImpl<SkuStockInOutDetailMapper, SkuStockInOutDetail>
    implements IService<SkuStockInOutDetail>, SkuStockInOutDetailService {

    @Autowired
    private SkuStockInOutDetailManager skuStockInOutDetailManager;
    @Autowired
    private SkuStockInOutDetailAssembly skuStockInOutDetailAssembly;
    
    @Override
    public IPage<SkuStockInOutDetailResp> pageDetail(SkuStockInOutDetailPageReq req) {
        LocalDateTime beginTime = req.getBeginLocalDateTime();
        LocalDateTime endTime = req.getEndLocalDateTime();
        LambdaQueryWrapper<SkuStockInOutDetail> lqw = SkuStockInOutDetail.lqw()
            .ge(Objects.nonNull(beginTime), SkuStockInOutDetail::getCreatedAt, beginTime)
            .le(Objects.nonNull(endTime), SkuStockInOutDetail::getCreatedAt, endTime)
            .orderByDesc(SkuStockInOutDetail::getCreatedAt);
        IPage<SkuStockInOutDetail> pageData = this.page(PageUtil.to(req), lqw);
        return PageUtil.to(pageData, skuStockInOutDetailAssembly::toSkuStockInOutDetailResp);
    }

    @Override
    public SkuStockInOutDetailResp getDetail(Long id) {
        SkuStockInOutDetail entity = this.getById(id);
    	return skuStockInOutDetailAssembly.toSkuStockInOutDetailResp(entity);
    }

    @Override
    public boolean saveDetail(SkuStockInOutDetailSaveReq req) {
        SkuStockInOutDetail entity = skuStockInOutDetailAssembly.toSkuStockInOutDetail(req);
    	return this.save(entity);
    }

    @Override
    public boolean updateDetail(SkuStockInOutDetailUpdateReq req) {
        SkuStockInOutDetail entity = skuStockInOutDetailAssembly.toSkuStockInOutDetail(req);
    	return this.updateById(entity);
    }

    @Override
    public boolean deleteDetail(Long id) {
    	return this.removeById(id);
    }
    
    @Override
    public boolean batchDeleteDetail(List<Long> ids) {
    	return this.removeByIds(ids);
    }
}
